//
//  HMMainTableViewController.swift
//  WeiBoXR
//
//  Created by acer on 16/9/19.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit
import SVProgressHUD
class HMMainTableViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
//        UITabBar.appearance().tintColor = UIColor.orange
        
        let hmTabBar = HMTabBar()
    
        hmTabBar.hmDelegate = self
        
        hmTabBar.addButtonClosure = { [weak self] in
//            print("xxx")
            if !HMUserAccountViewModel.sharedUserAccountViewModel.isLogin {
                SVProgressHUD.showError(withStatus: "请先登录, 亲~")
                return
            }
            if let target = self {
                let composeView = HMComposeView()
                composeView.show(target: target)
            }
            
        }
        setValue(hmTabBar, forKey: "tabBar")//继承关系要写对，否则找不到这个key
        
        addChildViewController(childController: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: HMProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    
    //重载下面这个方法
    func addChildViewController(childController: UIViewController, title: String , imageName: String) {
        
        //图片
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //文字
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)] ,for: .normal)
        
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        childController.title = title
        
        let nav = HMNavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    

}
//使用extension执行代理方法
extension HMMainTableViewController: HMTabBarDelegate {
    
    func didSelectedAddButton() {
        print("休息下")
        
    }
}
